//
//  WrongView.m
//  Boss
//
//  Created by 孙昕 on 16/1/14.
//  Copyright © 2016年 孙昕. All rights reserved.
//

#import "WrongView.h"
#import "Header.h"
#import "TextDrawView.h"
@interface WrongView()
{
    NSMutableArray<LevelStartData*> *arrItem;
    UIView *viewContent;
    UILabel *lbTitle;
    TextDrawView *viewDraw;
    UIScrollView *viewScroll;
    NSString *strFlag;
    NSInteger indexItem;
}
@end
@implementation WrongView
-(instancetype)init
{
    if(self=[super init])
    {
        arrItem=[[NSMutableArray alloc] initWithCapacity:30];
        viewContent=[[UIView alloc] initWithFrame:CGRectMake(40, 0, 280 , 380)];
        [self.contentView addSubview:viewContent];
        lbTitle=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 40)];
        lbTitle.textAlignment=NSTextAlignmentCenter;
        lbTitle.font=[UIFont systemFontOfSize:17];
        lbTitle.text=@"错题";
        lbTitle.backgroundColor=COL(52, 173, 252, 1);
        lbTitle.textColor=[UIColor whiteColor];
        [viewContent addSubview:lbTitle];
        viewScroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, 280, 340)];
        viewScroll.backgroundColor=[UIColor whiteColor];
        viewDraw=[[TextDrawView alloc] initWithFrame:CGRectMake(0, 0, 280, 340)];
        viewDraw.backgroundColor=[UIColor whiteColor];
        [viewScroll addSubview:viewDraw];
        [viewContent addSubview:viewScroll];
        viewScroll.translatesAutoresizingMaskIntoConstraints=NO;
        [viewContent addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[viewScroll]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewScroll)]];
        [viewContent addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[viewScroll]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewScroll)]];
        viewDraw.translatesAutoresizingMaskIntoConstraints=NO;
        [viewScroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[viewDraw(==280)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewDraw)]];
        [viewScroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[viewDraw]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewDraw)]];
        [viewDraw addConstraint:[NSLayoutConstraint constraintWithItem:viewDraw attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:340]];
        UIButton *btnPre=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        btnPre.text=@"<";
        btnPre.titleLabel.font=[UIFont systemFontOfSize:25];
        [btnPre setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnPre setBackgroundColor:[UIColor clearColor]];
        [viewContent addSubview:btnPre];
        [btnPre addTarget:self action:@selector(onPre:) forControlEvents:UIControlEventTouchUpInside];
        UIButton *btnNext=[[UIButton alloc] initWithFrame:CGRectMake(240, 0, 40, 40)];
        btnNext.text=@">";
        btnNext.titleLabel.font=[UIFont systemFontOfSize:25];
        [btnNext setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnNext setBackgroundColor:[UIColor clearColor]];
        [viewContent addSubview:btnNext];
        [btnNext addTarget:self action:@selector(onNext:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(void)onPre:(UIButton*)btn
{
    if(indexItem==0)
    {
        return;
    }
    indexItem--;
    [self drawText:arrItem[indexItem]];
}

-(void)onNext:(UIButton*)btn
{
    if(indexItem==arrItem.count-1)
    {
        return;
    }
    indexItem++;
    [self drawText:arrItem[indexItem]];
}

-(void)addItem:(LevelStartData*)item Flag:(NSString *)flag
{
    [arrItem addObject:item];
    strFlag=flag;
    indexItem=arrItem.count-1;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[touches anyObject];
    CGPoint p=[touch locationInView:self];
    if(!CGRectContainsPoint(viewContent.frame, p))
    {
        if(_delegate && [_delegate respondsToSelector:@selector(WrongViewDismiss)])
        {
            [_delegate WrongViewDismiss];
        }
        [UIView animateWithDuration:0.3 animations:^{
            self.effect=nil;
            self.contentView.alpha=0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
}

-(void)showInView:(UIView*)view
{
    self.frame=view.frame;
    [view addSubview:self];
    self.contentView.alpha=1;
    [UIView animateWithDuration:0.2 animations:^{
        UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        self.effect=blur;
    }];
    viewContent.frame=CGRectMake(([UIScreen mainScreen].bounds.size.width-viewContent.bounds.size.width)/2, [UIScreen mainScreen].bounds.size.height, viewContent.bounds.size.width, viewContent.bounds.size.height);
    [UIView animateWithDuration:0.3 delay:0.1 usingSpringWithDamping:0.6 initialSpringVelocity:15 options:0 animations:^{
        viewContent.center=self.center;
    } completion:nil];
    [self drawText:[arrItem lastObject]];
}

-(void)drawText:(LevelStartData*)item
{
    [viewDraw clear];
    [viewDraw setDrawInset:10 Top:10];
    NSArray* arr=[item.content componentsSeparatedByString:strFlag];
    NSInteger index=0;
    for(int i=0;i<arr.count;i++)
    {
        [viewDraw addText:arr[i] Style:@{NSFontAttributeName:[UIFont systemFontOfSize:20]} ClickBlock:nil];
        if(i==arr.count-1)
        {
            break;
        }
        [viewDraw addText:((LevelStartAnswer*)item.answer[index++]).ok Style:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSBackgroundColorAttributeName:[UIColor greenColor]} ClickBlock:nil];
    }
    [viewDraw setNeedsLayout];
    [viewDraw setNeedsDisplay];
    lbTitle.text=[NSString stringWithFormat:@"错题(%ld/%ld)",indexItem+1,arrItem.count];
}
@end





